package top.carrotguo.weibo.utils;

import android.text.format.DateFormat;

import com.orhanobut.logger.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 微博日期提取转换的工具类
 */
public class DateUtils {
    //日期格式：Sat Nov 10 16:35:15 +0800 2018

    /*一分钟毫秒*/
    public static final long ONE_MINUTE_MILLIONS = 60*1000;
    /*一小时的毫秒*/
    public static final long ONE_HOUR_MILLIONS = ONE_MINUTE_MILLIONS * 60;
    /*一天的毫秒*/
    public static final long ONE_DAY_MILLIONS = ONE_HOUR_MILLIONS * 24;

    public static String getShortTime(String dataStr){
        String result = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",Locale.US);
        try {
            Date targetDate = simpleDateFormat.parse(dataStr);
            Date currentDate = new Date();

            //获取现在与发布时间的间隔毫秒数
            long durTime = currentDate.getTime() - targetDate.getTime();
            int dayStatus = caculateDayStatus(targetDate,currentDate);
            int yearStatus = caculateYearStatus(targetDate,currentDate);

            if (durTime <= ONE_MINUTE_MILLIONS*10) {
                //十分钟内发布的都设置为刚刚
                result = "刚刚";
            } else if (durTime < ONE_HOUR_MILLIONS) {
                //一小时内的设置为*分钟前
                result = durTime/ONE_MINUTE_MILLIONS+"分钟前";
            } else if (dayStatus == 0) {
                //一天内设置为*小时前
                result = durTime/ONE_HOUR_MILLIONS+"小时前";
            } else if (dayStatus == 1) {
                //设置为昨天
                result = "昨天";
            } else if (yearStatus == 0) {
                //同一年发布，则显示月日
                result = DateFormat.format("MM-dd",targetDate).toString();
            } else {
                //非同年发布，显示年月日
                result = DateFormat.format("yyyy-MM",targetDate).toString();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getLongTime(String dataStr){
        //Wed Jun 01 00:50:25 +0800 2011
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",Locale.US);
        String result = "";
        try {
            Date targetDate = simpleDateFormat.parse(dataStr);
            Date currentDate = new Date();
            int yearStates = caculateYearStatus(targetDate,currentDate);
            SimpleDateFormat longFormat;
            if (yearStates == 0) {
                //同年，不需要显示年份
                longFormat = new SimpleDateFormat("MM-dd HH:mm",Locale.US);
            } else {
                //不同年，显示年份
                longFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.US);
            }
            result = longFormat.format(targetDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 判断今天与发布时间的间隔
     * @param targetTime
     * @param currentTime
     * @return
     */
    private static int caculateDayStatus(Date targetTime,Date currentTime){
        Calendar targetCalendar = Calendar.getInstance();
        targetCalendar.setTime(targetTime);
        int targetDay = targetCalendar.get(Calendar.DAY_OF_YEAR);

        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.setTime(currentTime);
        int currentDay = currentCalendar.get(Calendar.DAY_OF_YEAR);

        return currentDay - targetDay;
    }

    /**
     * 用于判断是否发布时间为同一年
     * @param targetTime
     * @param currentTime
     * @return
     */
    private static int caculateYearStatus(Date targetTime,Date currentTime){
        Calendar targetCalendar = Calendar.getInstance();
        targetCalendar.setTime(targetTime);
        int targetYear = targetCalendar.get(Calendar.YEAR);

        Calendar currentCalendar = Calendar.getInstance();
        currentCalendar.setTime(currentTime);
        int currentYear = currentCalendar.get(Calendar.YEAR);

        return currentYear - targetYear;
    }


}
